package com.foo.ReadDataTest;

import java.io.File;
import java.util.Calendar;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import junit.framework.Assert;

import org.junit.Test;

import com.foo.SnapshotData.SnapshotDataProviderRemote;
import com.foo.SpatialData.SpatialDataProviderRemote;
import com.foo.TemporalData.TemporalDataProviderRemote;
import com.foo.XMLWriter.XMLWriterClient;
import com.foo.AnalyzedData.AnalyzedDataProviderRemote;
import com.foo.CSVWriter.CSVWriterClient;
import com.foo.JSONWriter.JSONWriterClient;
import com.foo.ReadData.DataReceiver;

public class ReadDataTest 
{
	@Test
	public void readdatatest()
	{
		SpatialDataProviderRemote dataprovider = null;
			try
			{
				Properties properties = new Properties();
				properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
				properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
				properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
				Context ctx = new InitialContext(properties);
				Object ref = ctx.lookup("SpatialDataProvider/remote");
				//DataProviderRemote 
				dataprovider = (SpatialDataProviderRemote) PortableRemoteObject.narrow(ref, SpatialDataProviderRemote.class);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			DataReceiver recieve=new DataReceiver();
			dataprovider.SpacialData("Country", "Ireland");
			String[] test=recieve.readtickerinfodata();
			
			String file = "/Users/kausiksridharabalan/Desktop/data.csv";
			
			
			CSVWriterClient client = new CSVWriterClient();
			client.generateCSV("/Users/kausiksridharabalan/Desktop/data.csv", test,"Spatial");
			
			File f = new File(file);
			if(f.exists())
			{
				System.out.println("File Created");			
			}
			else
			{
				System.out.println("File Create failed");			
			}
			int lengthexpected = (test.length)/8;
			
			Assert.assertEquals(4, lengthexpected);
	}
	@Test
	public void readdatatest_1()
	{
		SpatialDataProviderRemote dataprovider = null;
			try
			{
				Properties properties = new Properties();
				properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
				properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
				properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
				Context ctx = new InitialContext(properties);
				Object ref = ctx.lookup("SpatialDataProvider/remote");
				//DataProviderRemote 
				dataprovider = (SpatialDataProviderRemote) PortableRemoteObject.narrow(ref, SpatialDataProviderRemote.class);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			DataReceiver recieve=new DataReceiver();
			dataprovider.SpacialData("Country", "India");
			String[] test=recieve.readtickerinfodata();
			int lengthexpected = (test.length)/8;
			
			Assert.assertEquals(0, lengthexpected);
	}
	@Test
	public void readdatatest_2()
	{
		SnapshotDataProviderRemote dataprovider = null;
			try
			{
				Properties properties = new Properties();
				properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
				properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
				properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
				Context ctx = new InitialContext(properties);
				Object ref = ctx.lookup("SnapshotDataProvider/remote");
				//DataProviderRemote 
				dataprovider = (SnapshotDataProviderRemote) PortableRemoteObject.narrow(ref, SnapshotDataProviderRemote.class);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			java.util.Date date = new java.util.Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_YEAR, -1000);
			java.util.Date fromDate = calendar.getTime();
			Calendar calendar1 = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_YEAR, -993);
			java.util.Date toDate = calendar1.getTime();
			java.sql.Date sqltoDate = new java.sql.Date(toDate.getTime());
			java.sql.Date sqlfromDate = new java.sql.Date(fromDate.getTime());


			DataReceiver recieve=new DataReceiver();
			dataprovider.sendsnapshotinfo("GE", "OneWeek",sqlfromDate, sqltoDate);
			String[] test=recieve.readtickerinfodata();
			String file = "/Users/kausiksridharabalan/Desktop/data.txt";
			
			
			CSVWriterClient client = new CSVWriterClient();
			client.generateCSV("/Users/kausiksridharabalan/Desktop/data.txt", test,"Snapshot");
			
			File f = new File(file);
			if(f.exists())
			{
				System.out.println("File Created");			
			}
			else
			{
				System.out.println("File Create failed");			
			}			
			Assert.assertNotNull(test);
			//Assert.assertEquals(0, lengthexpected);
	}
	@Test
	public void readdatatest_3()
	{
		SnapshotDataProviderRemote dataprovider = null;
			try
			{
				Properties properties = new Properties();
				properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
				properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
				properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
				Context ctx = new InitialContext(properties);
				Object ref = ctx.lookup("SnapshotDataProvider/remote");
				//DataProviderRemote 
				dataprovider = (SnapshotDataProviderRemote) PortableRemoteObject.narrow(ref, SnapshotDataProviderRemote.class);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			java.util.Date date = new java.util.Date();
			java.util.Date date1 = new java.util.Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date1);
			calendar.add(Calendar.DAY_OF_YEAR, -7);
			java.util.Date fromDate = calendar.getTime();
			java.sql.Date sqltoDate = new java.sql.Date(fromDate.getTime());
			java.sql.Date sqlfromDate = new java.sql.Date(date.getTime());


			DataReceiver recieve=new DataReceiver();
			dataprovider.sendsnapshotinfo("GE", "OneWeek",sqlfromDate, sqltoDate);
			String[] test=recieve.readtickerinfodata();
			int lengthexpected = (test.length)/6;
			
		    Assert.assertEquals(0, lengthexpected);
	}
	@Test
	public void readdatatest_4()
	{
		TemporalDataProviderRemote dataprovider = null;
			try
			{
				Properties properties = new Properties();
				properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
				properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
				properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
				Context ctx = new InitialContext(properties);
				Object ref = ctx.lookup("TemporalDataProvider/remote");
				//DataProviderRemote 
				dataprovider = (TemporalDataProviderRemote) PortableRemoteObject.narrow(ref, TemporalDataProviderRemote.class);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			java.util.Date date = new java.util.Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_YEAR, -1000);
			java.util.Date fromDate = calendar.getTime();
			Calendar calendar1 = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_YEAR, -993);
			java.util.Date toDate = calendar1.getTime();
			java.sql.Date sqltoDate = new java.sql.Date(toDate.getTime());
			java.sql.Date sqlfromDate = new java.sql.Date(fromDate.getTime());


			DataReceiver recieve=new DataReceiver();
			dataprovider.sendtemporalinfo("GE",sqlfromDate, sqltoDate);
			String[] test=recieve.readtickerinfodata();

			String file = "/Users/kausiksridharabalan/Desktop/data.json";

			JSONWriterClient client = new JSONWriterClient();
			client.generateJSON("/Users/kausiksridharabalan/Desktop/data.json", test,"Temporal");
			
			File f = new File(file);
			if(f.exists())
			{
				System.out.println("File Created");			
			}
			else
			{
				System.out.println("File Create failed");			
			}	
			
			Assert.assertNotNull(test);
			//Assert.assertEquals(0, lengthexpected);
	}
	@Test
	public void readdatatest_5()
	{
		TemporalDataProviderRemote dataprovider = null;
			try
			{
				Properties properties = new Properties();
				properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
				properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
				properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
				Context ctx = new InitialContext(properties);
				Object ref = ctx.lookup("TemporalDataProvider/remote");
				//DataProviderRemote 
				dataprovider = (TemporalDataProviderRemote) PortableRemoteObject.narrow(ref, TemporalDataProviderRemote.class);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			java.util.Date date = new java.util.Date();
			java.util.Date date1 = new java.util.Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date1);
			calendar.add(Calendar.DAY_OF_YEAR, -7);
			java.util.Date fromDate = calendar.getTime();
			java.sql.Date sqltoDate = new java.sql.Date(fromDate.getTime());
			java.sql.Date sqlfromDate = new java.sql.Date(date.getTime());


			DataReceiver recieve=new DataReceiver();
			dataprovider.sendtemporalinfo("GE",sqlfromDate, sqltoDate);
			String[] test=recieve.readtickerinfodata();
			int lengthexpected = (test.length)/6;	
		    Assert.assertEquals(0, lengthexpected);
	}
	@Test
	public void readdatatest_6()
	{
		AnalyzedDataProviderRemote dataprovider = null;
			try
			{
				Properties properties = new Properties();
				properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
				properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
				properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
				Context ctx = new InitialContext(properties);
				Object ref = ctx.lookup("AnalyzedDataProvider/remote");
				//DataProviderRemote 
				dataprovider = (AnalyzedDataProviderRemote) PortableRemoteObject.narrow(ref, AnalyzedDataProviderRemote.class);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			DataReceiver recieve=new DataReceiver();
			dataprovider.Analyzedinfo("TopVolume");
			String[] test=recieve.readtickerinfodata();
			String file = "/Users/kausiksridharabalan/Desktop/data.xml";

			XMLWriterClient client = new XMLWriterClient();
			client.generateJSON("/Users/kausiksridharabalan/Desktop/data", test,"Analyzed");
			
			File f = new File(file);
			if(f.exists())
			{
				System.out.println("File Created");			
			}
			else
			{
				System.out.println("File Create failed");			
			}	
			Assert.assertNotNull(test);
	}
	@Test
	public void readdatatest_7()
	{
		AnalyzedDataProviderRemote dataprovider = null;
			try
			{
				Properties properties = new Properties();
				properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
				properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
				properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
				Context ctx = new InitialContext(properties);
				Object ref = ctx.lookup("AnalyzedDataProvider/remote");
				//DataProviderRemote 
				dataprovider = (AnalyzedDataProviderRemote) PortableRemoteObject.narrow(ref, AnalyzedDataProviderRemote.class);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
			
			DataReceiver recieve=new DataReceiver();
			dataprovider.Analyzedinfo("ABC");
			String[] test=recieve.readtickerinfodata();
			int lengthexpected = (test.length)/6;	
		    Assert.assertEquals(0, lengthexpected);
	}	
}
